package org.dtrd.modules.shop.service.impl;

import cn.hutool.http.body.RequestBody;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.config.system.Constant;
import org.dtrd.modules.shop.entity.bean.*;
import org.dtrd.modules.shop.entity.po.DtrdEntShcRdPatientAddress;
import org.dtrd.modules.shop.mapper.DtrdEntShcRdPatientAddressMapper;
import org.dtrd.modules.shop.service.IDtrdEntShcRdPatientAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.dynamic.datasource.annotation.DS;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 * 收货地址表 服务实现类
 * </p>
 *
 * @author sun
 * @since 2022-07-23
 */

@DS("multi-datasource1")
@Slf4j
@Service
public class DtrdEntShcRdPatientAddressServiceImpl extends ServiceImpl<DtrdEntShcRdPatientAddressMapper, DtrdEntShcRdPatientAddress> implements IDtrdEntShcRdPatientAddressService {

    @Override
    public List<PatientAddressListInfo> getPatientAddressList(PatientAddressListRequest request) {
        LambdaQueryWrapper<DtrdEntShcRdPatientAddress> queryWrapper = baseQueryWrapper();
        List<DtrdEntShcRdPatientAddress> poList = list(queryWrapper);
        List<PatientAddressListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(PatientAddressListInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public IPage<PatientAddressListInfo> pagePatientAddress(PatientAddressPageRequest request) {
        LambdaQueryWrapper<DtrdEntShcRdPatientAddress> queryWrapper = baseQueryWrapper();
        IPage<DtrdEntShcRdPatientAddress> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = this.page(page, queryWrapper);
        return page.convert(PatientAddressListInfo::new);
    }

    @Override
    public PatientAddressDetail getPatientAddress(Integer dataId) {
        DtrdEntShcRdPatientAddress dtrdEntShcRdPatientAddress = getById(dataId);
        return Optional.ofNullable(dtrdEntShcRdPatientAddress)
                        .map(po -> new PatientAddressDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdatePatientAddress(PatientAddressDetail dto) {
        DtrdEntShcRdPatientAddress dtrdEntShcRdPatientAddress = new DtrdEntShcRdPatientAddress();
        if(dto.getDataId() == null){
            dtrdEntShcRdPatientAddress = dtrdEntShcRdPatientAddress.init();
        }
        dtrdEntShcRdPatientAddress.parseFromDto(dto);
        return saveOrUpdate(dtrdEntShcRdPatientAddress);
    }

    @Override
    public boolean setDefaultAddress(PatientAddressSetDefaultRequest request) {
        LambdaUpdateWrapper<DtrdEntShcRdPatientAddress> updateNoWrapper = Wrappers.lambdaUpdate(DtrdEntShcRdPatientAddress.class)
                .eq(DtrdEntShcRdPatientAddress::getPatientId, request.getPatientId())
                .set(true, DtrdEntShcRdPatientAddress::getIsDefault, Constant.DEFAULT_ADDRESS.NO.getType());
        boolean res = this.update(updateNoWrapper);
        log.info("res===" + res);
        if (res) {
            DtrdEntShcRdPatientAddress dtrdEntShcRdPatientAddress = getById(request.getDataId());
            dtrdEntShcRdPatientAddress.setIsDefault(Constant.DEFAULT_ADDRESS.YES.getType());
            return updateById(dtrdEntShcRdPatientAddress);
        }
        return false;
    }

    @Override
    public boolean removePatientAddress(Integer dataId) {
        return removeById(dataId);
    }

    private LambdaQueryWrapper<DtrdEntShcRdPatientAddress> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntShcRdPatientAddress.class)
                        .eq(DtrdEntShcRdPatientAddress::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntShcRdPatientAddress> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntShcRdPatientAddress.class)
                        .eq(DtrdEntShcRdPatientAddress::getIsDel, 0);
    }
}
